package com.school.cooperation.entity;

import com.school.cooperation.entity.enums.UserRole;
import com.school.cooperation.entity.enums.UserStatus;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;

/**
 * 用户实体类
 *
 * @author system
 * @since 2025-11-15
 */
@Data
@SuperBuilder
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "sys_user")
public class User extends BaseEntity {

    /**
     * 用户名
     */
    @NotBlank(message = "用户名不能为空")
    @Size(min = 3, max = 50, message = "用户名长度必须在3-50个字符之间")
    @Column(name = "username", nullable = false, unique = true, length = 50)
    private String username;

    /**
     * 密码（加密后）
     */
    @NotBlank(message = "密码不能为空")
    @Size(min = 6, max = 100, message = "密码长度必须在6-100个字符之间")
    @Column(name = "password", nullable = false, length = 100)
    private String password;

    /**
     * 真实姓名
     */
    @NotBlank(message = "真实姓名不能为空")
    @Size(max = 50, message = "真实姓名长度不能超过50个字符")
    @Column(name = "real_name", nullable = false, length = 50)
    private String realName;

    /**
     * 手机号
     */
    @Size(max = 20, message = "手机号长度不能超过20个字符")
    @Column(name = "phone", unique = true, length = 20)
    private String phone;

    /**
     * 邮箱
     */
    @Email(message = "邮箱格式不正确")
    @Size(max = 100, message = "邮箱长度不能超过100个字符")
    @Column(name = "email", length = 100)
    private String email;

    /**
     * 学校ID
     */
    @Column(name = "school_id")
    private String schoolId;



    /**
     * 学校ID
     */
    @Column(name = "school_name")
    private String schoolName;

    /**
     * 头像URL
     */
    @Size(max = 255, message = "头像URL长度不能超过255个字符")
    @Column(name = "avatar", length = 255)
    private String avatar;

    /**
     * 用户角色
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "role", nullable = false, length = 20)
    private UserRole role;

    /**
     * 用户状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private UserStatus status = UserStatus.ACTIVE;

    /**
     * 最后登录时间
     */
    @Column(name = "last_login_time")
    private LocalDateTime lastLoginTime;

    /**
     * 是否为管理员
     */
    @Transient
    public boolean isAdmin() {
        return UserRole.ADMIN.equals(role);
    }

    /**
     * 是否为教师角色
     */
    @Transient
    public boolean isTeacher() {
        return UserRole.TEACHER.equals(role);
    }

    /**
     * 是否为家长角色
     */
    @Transient
    public boolean isParent() {
        return UserRole.PARENT.equals(role);
    }

    /**
     * 是否为学校管理员
     */
    @Transient
    public boolean isSchoolAdmin() {
        return UserRole.SCHOOL_ADMIN.equals(role);
    }

    /**
     * 账户是否可用
     */
    @Transient
    public boolean isAccountNonLocked() {
        return !UserStatus.SUSPENDED.equals(status);
    }

    /**
     * 账户是否启用
     */
    @Transient
    public boolean isEnabled() {
        return UserStatus.ACTIVE.equals(status);
    }
}